﻿var testEmail = /^[A-Za-z0-9](([_\.\+\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/;
var testMobile = /^(13[0-9]|14[0-9]|15[0|1|2|3|5|6|7|8|9]|18[0-9])\d{8}$/;

//核对邮箱格式
function checkEmail(email){
	if(!testEmail.test(email)){
		return false;
	}
	return true;
}

//核对手机格式
function checkMobile(mobile){
	if(!testMobile.test(mobile)){
		return false;
	}
	return true;
}



//数组排序
function bubbleSortF(array){
	var i = 0, len = array.length, j, d; 
	for(; i<len; i++){
		for(j=0; j<len; j++){
			if(array[i] < array[j]){
				d = array[j]; 
				array[j] = array[i]; 	
				array[i] = d;
			}
		}
	}
	 return array; 
}

//倒计时函数
function countdown(button_obj,button_text,original_class,bzzzik_class,bzzzik_time){
	var startTime = new Date().getTime();
	var InterValObj;
	
	//倒计时按钮禁用和样式调整
	button_obj.attr("disabled","disabled");
    button_obj.attr('class',bzzzik_class);
	//获取倒计时的起始时间
    button_obj.attr('value',bzzzik_time + "秒");
    //定时器,1秒执
	InterValObj = window.setInterval(SetRemainTime,1000); 
	//将时间减去1秒
	function SetRemainTime(){
		var milsLeft = startTime + bzzzik_time*1000 - new Date().getTime();
		if (milsLeft > 0) {
			button_obj.attr('value',Math.ceil(milsLeft / 1000) + "秒");
		} else {//剩余时间小于或等于0的时候，就停止间隔函数
			window.clearInterval(InterValObj);
			//这里可以添加倒计时时间为0后需要执行的事件
	        button_obj.attr('value',button_text);
			button_obj.attr('class',original_class);
			button_obj.removeAttr('disabled');
		}
	}
}

//公共信息提示
function msg_tit_common(msg,class_name,scroll_status,animate_status){
	if(msg){
		$("."+class_name).html(msg);
		$("."+class_name).show();
		if(animate_status){
			error_msg_ani(class_name);
		}
		if(scroll_status){
			$('body,html').animate({scrollTop:0},500);
		}
	}else{
		$("."+class_name).html('');
		$("."+class_name).hide();
	}
}

//错误信息晃动效果
function error_msg_ani(class_name){
	$("."+class_name).addClass("entryamt");
	setTimeout("$('."+class_name+"').removeClass('entryamt')",500);
}

function getUrlParam(key, url) {
	url = url || location.href;
	var pos = url.indexOf('?');
	if (pos == -1)  {
		return key ? '' : {};
	}
	
	url = url.substr(pos + 1);
	
	var url_arr = url.split('&');
	var gets = {};
	for (var i in url_arr) {
		var param = url_arr[i].split('=');
		gets[decodeURIComponent(param[0])] = decodeURIComponent(param[1] || '');
	}
	
	if (!key) {
		return gets;
	} else {
		return gets[key] || '';
	}
}

function simulateForm(url, params, method, ajax) {
	method = (method || '').toLowerCase() == 'get' ? 'get' : 'post';
	
	if ($.trim(url) == '') return false;
	if (Object.prototype.toString.call(params) != '[object Object]') return false;
	
	if (method == 'get') {
		var url_params = getUrlParam('', url);
		params = $.extend({}, url_params, params);
	}
	
	if (ajax) {
		var xhr = $.ajax({url: url, type: method, async: false, data: params});
		var text = xhr.responseText ? xhr.responseText : xhr.responseXML;
		return text ? text : '';
	}
	
	var simform = $('<form action="'+url+'" onsubmit="return simulateForm.direct_submit(event);" method="'+method+'">');
	for (var key in params) {
		simform.append('<input type="hidden" name="' + key + '" value="' + params[key] + '" />');
	}
	simform.appendTo('body').submit();
	return true;
}
simulateForm.direct_submit = function(event) {
	event = event || window.event;
	event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation();
	return true;
};